import { Personage } from '@libs/db/models/personage.model';
import { Works } from '@libs/db/models/works.model';
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ReturnModelType } from '@typegoose/typegoose';
import { Crud } from 'nestjs-mongoose-crud';
import { InjectModel } from 'nestjs-typegoose';

@Crud({
    model: Personage
})
@Controller('personages')
@ApiTags('人物')
export class PersonagesController {

    constructor(
        @InjectModel(Personage) private readonly model: ReturnModelType<typeof Personage>,
        @InjectModel(Works) private readonly worksModel: ReturnModelType<typeof Works>
    ) { }

    @Get('option')
    async option() {
        const works = (await this.worksModel.find()).map(v => ({
            label: v.title,
            value: v._id
        }))
        return {
            title: '人物管理',
            border: true,
            searchMenuSpan: 8,
            column: [
                {
                    label: '名字',
                    prop: 'name',
                    search: true,
                    regex: true,
                    row: true
                },
                {
                    label: '头像',
                    prop: 'portrait',
                    type: 'upload',
                    listType: 'picture-img',
                    tip: '只能上传jpg/png用户头像，且不超过500kb',
                    row: true,
                    action: 'http://localhost:3009/upload'
                },
                {
                    label: '作品',
                    prop: 'works',
                    type: "select",
                    multiple:true,
                    dicData: works,
                    dataType: 'array',
                    row: true
                },
                {
                    label: '综述',
                    prop: 'introduce'
                }
            ]
        }
    }
}
